package cn.tedu.egmvc6.controller;

import cn.tedu.egmvc6.base.response.JsonResult;
import cn.tedu.egmvc6.base.response.StateCode;
import cn.tedu.egmvc6.mapper.WeiboMapper;
import cn.tedu.egmvc6.pojo.dto.WeiboContentParam;
import cn.tedu.egmvc6.pojo.entity.Weibo;
import cn.tedu.egmvc6.pojo.vo.UserVO;
import cn.tedu.egmvc6.pojo.vo.WeiboListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(tags = "02.微博管理")
@RestController
@RequestMapping("/v1/weibo/")
public class WeiboController {
    //发表微博
    @Autowired
    private WeiboMapper weiboMapper;
    @ApiOperation("发布微博")
    @ApiOperationSupport(order = 10)
    @PostMapping("insert")
    public JsonResult insert(@RequestBody WeiboContentParam weiboContentParam , @ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO ==null){
            return new JsonResult(StateCode.NOT_LOGIN);
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboContentParam ,weibo);
        weibo.setCreateTime(new Date());
        weibo.setUserId(userVO.getId());
        weiboMapper.insert(weibo);
        return JsonResult.ok();
    }
    //微博首页列表
    @ApiOperation("微博首页列表")
    @ApiOperationSupport(order = 20)
    @GetMapping("selectIndex")
    public JsonResult weiboList(){
        List<WeiboListVO> weiboListVO =weiboMapper.weiboList();
        for (WeiboListVO weiboListVO1 : weiboListVO){
            System.out.println(weiboListVO1);
            return JsonResult.ok();
        }
        return new JsonResult(StateCode.VALIDATED_ERROR);
    }
}
